package com.buli.blmall.admin.common.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.buli.blmall.admin.consts.CacheKey;
import com.buli.blmall.admin.exception.SmsException;
import com.buli.blmall.admin.exception.error.SmsErrorCode;
import com.buli.blmall.admin.captcha.MatchCaptcha;
import com.buli.blmall.admin.core.cache.RedisClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.awt.*;

/**
 * 验证码服务
 * @author xiang.gao
 * @date 2024/12/20 16:18
 */
@Service
@Slf4j
@AllArgsConstructor
public class CaptchaService {

    private final RedisClient redisClient;

    /**
     * 生成验证码
     * @param id
     * @return
     */
    public String genCode(String id) {
        String cacheKey = CacheKey.CAPTCHA_CODE_KEY.concat(id);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 50, 4, 5);
        lineCaptcha.setBackground(Color.LIGHT_GRAY);
        log.info("生成的验证码, id:{}, code:{}", id, lineCaptcha.getCode());
        redisClient.setValue(cacheKey, lineCaptcha.getCode(), CacheKey.CAPTCHA_CODE_EXPIRE_TIME);
        return lineCaptcha.getImageBase64Data();
    }

    /**
     * 生成计算验证码
     * @param uuid
     * @return
     */
    public String genMatchCode(String uuid) {
        MatchCaptcha matchCaptcha = new MatchCaptcha();
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 50, 4, 5);
        captcha.setGenerator(matchCaptcha);
        captcha.createCode();
        String cacheKey = CacheKey.CAPTCHA_CODE_KEY.concat(uuid);
        redisClient.setValue(cacheKey, String.valueOf(matchCaptcha.getAnswer()), CacheKey.CAPTCHA_CODE_EXPIRE_TIME);
        log.info("生成的验证码,uuid:{} code:{},计算结果:{}", uuid, captcha.getCode(), matchCaptcha.getAnswer());
        return captcha.getImageBase64Data();
    }

    /**
     * 获取验证码
     * @param uuid
     * @return
     */
    public String getImageCode(String uuid) {
        if (StringUtils.isBlank(uuid)) {
            return null;
        }
        String cacheKey = CacheKey.CAPTCHA_CODE_KEY.concat(uuid);
        String imageCode = redisClient.getValue(cacheKey);
        log.info("获取验证码, uuid:{},code:{}", uuid, imageCode);
        return imageCode;
    }

    /**
     * 移除验证码
     * @param uuid
     */
    public void removeImageCode(String uuid) {
        if (StringUtils.isBlank(uuid)) {
            return;
        }
        String cacheKey = CacheKey.CAPTCHA_CODE_KEY.concat(uuid);
        redisClient.deleteValue(cacheKey);
    }

    /**
     * 校验验证码
     * @param uuid
     * @param code
     * @return
     */
    public boolean verify(String uuid, String code) {
        log.info("校验验证码,uuid:{},code:{}", uuid, code);
        if (StringUtils.isBlank(uuid) || StringUtils.isBlank(code)) {
            return false;
        }
        String cacheKey = CacheKey.CAPTCHA_CODE_KEY.concat(uuid);
        String imageCode = redisClient.getValue(cacheKey);
        if (StringUtils.isBlank(imageCode)) {
            throw new SmsException(SmsErrorCode.CAPTCHA_EXPIRE);
        }
        if (code.equals(imageCode)) {
            redisClient.deleteValue(cacheKey);
            return true;
        }
        throw new SmsException(SmsErrorCode.CAPTCHA_ERROR);
    }

}
